package org.insightech.er.editor.model.dbexport.html.page_generator.impl;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.insightech.er.ResourceString;
import org.insightech.er.editor.model.ERDiagram;
import org.insightech.er.editor.model.dbexport.html.ExportToHtmlManager;
import org.insightech.er.editor.model.dbexport.html.page_generator.AbstractHtmlReportPageGenerator;
import org.insightech.er.editor.model.diagram_contents.element.node.table.ERTable;
import org.insightech.er.editor.model.diagram_contents.element.node.table.TableView;
import org.insightech.er.editor.model.diagram_contents.not_element.tablespace.Tablespace;
import org.insightech.er.editor.model.diagram_contents.not_element.tablespace.TablespaceProperties;
import org.insightech.er.editor.model.settings.Environment;
import org.insightech.er.util.Format;

public class TablespaceHtmlReportPageGenerator extends AbstractHtmlReportPageGenerator {

    public TablespaceHtmlReportPageGenerator(final Map<Object, Integer> idMap) {
        super(idMap);
    }

    @Override
    public String getType() {
        return "tablespace";
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Object> getObjectList(final ERDiagram diagram) {
        final List list = diagram.getDiagramContents().getTablespaceSet().getObjectList();

        return list;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String[] getContentArgs(final ERDiagram diagram, final Object object) throws IOException {
        final Tablespace tablespace = (Tablespace) object;

        final String environments = generateEnvironmentTable(diagram, tablespace);

        final Tablespace defaultTablespace = diagram.getDiagramContents().getSettings().getTableViewProperties().getTableSpace();

        final List<TableView> usedTableList = new ArrayList<TableView>();

        for (final ERTable table : diagram.getDiagramContents().getContents().getTableSet()) {
            final Tablespace useTablespace = table.getTableViewProperties().getTableSpace();
            if (useTablespace == null) {
                if (defaultTablespace == tablespace) {
                    usedTableList.add(table);
                }
            } else {
                if (useTablespace == tablespace) {
                    usedTableList.add(table);
                }
            }
        }

        final String usedTableTable = generateUsedTableTable(usedTableList);

        return new String[] {environments, usedTableTable};
    }

    @Override
    public String getObjectName(final Object object) {
        final Tablespace tablespace = (Tablespace) object;

        return tablespace.getName();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getObjectSummary(final Object object) {
        return null;
    }

    private String generateEnvironmentTable(final ERDiagram diagram, final Tablespace tablespace) throws IOException {
        final StringBuilder sb = new StringBuilder();

        final String template = ExportToHtmlManager.getTemplate("types/environment_row_template.html");

        for (final Environment environment : diagram.getDiagramContents().getSettings().getEnvironmentSetting().getEnvironments()) {
            final TablespaceProperties properties = tablespace.getPropertiesMap().get(environment);
            if (properties == null) {
                continue;
            }

            final Object[] args = {Format.null2blank(environment.getName()), generateValueTable(properties)};
            final String row = MessageFormat.format(template, args);

            sb.append(row);
        }

        return sb.toString();
    }

    private String generateValueTable(final TablespaceProperties properties) throws IOException {
        final StringBuilder sb = new StringBuilder();

        final String template = ExportToHtmlManager.getTemplate("types/value_row_template.html");

        for (final Map.Entry<String, String> entry : properties.getPropertiesMap().entrySet()) {
            final Object[] args = {ResourceString.getResourceString(entry.getKey()), Format.null2blank(entry.getValue())};
            final String row = MessageFormat.format(template, args);

            sb.append(row);
        }

        return sb.toString();
    }

}
